package threads;

import java.io.IOException;
import java.io.InputStreamReader;

public class Starter {

	private static volatile boolean stopRequested = false;
	
	public static void main(String[] args) {	
		
		Runnable r1 = new Runnable() {
			  public void run() {
			    try {
			      while (!stopRequested) {
			        System.out.println("Hello, world!");
			        Thread.sleep(1000L);
			      }
			    } catch (InterruptedException iex) {}
			  }
			};
			Runnable r2 = new Runnable() {
			  public void run() {
				 
			    try {
			    	InputStreamReader isr = new InputStreamReader(System.in);  
			    	
			      while (!stopRequested) {
			    	
			  		if ( isr.read() == 13) {
			  			stopRequested = false;
			  		}
			    	  
			        System.out.println("Goodbye, " +
					"cruel world!");
			        Thread.sleep(2000L);
			      }
			    } catch (InterruptedException iex) {} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			  }
			};
	Thread thr1 = new Thread(r1);
	Thread thr2 = new Thread(r2);
	thr1.start();
	thr2.start();

	}

}
